
function subsampleFemaleWrongConfident = allSubjects (subjectList)


%subject='laura_test_Adaptive_50ms_300trials'; %laura_tpp25ms_22_29_300';

qtyTrials=350;%300;%20;
sequenceSize=20;%40;
%maleStart=18; %22;%21;
%femaleStart=28;%29;%30; 



allSubs={'nico2'; 'pablo'; 'mariano'; 'mercedes'; 'mabel'; 'cesar'; 'federico'; 'rosario'; 'alberto'};
names=cell(1,length(subjectList));
for nam=1:length(subjectList)
    names{nam}=allSubs{subjectList(nam)};
   
end

allMaleStarts=[18 18 25 30 14 27 14 25 18];
allFemaleStarts=[28 28 35 40 24 37 24 35 28];

maleStarts=allMaleStarts(subjectList);
femaleStarts=allFemaleStarts(subjectList);


nSub=length(names);
data=cell(1,nSub);

for sub=1:nSub
    filename=strcat('RAW_',names{sub})
    
    file=fopen(filename); 
    data{sub}=fread(file, [4+sequenceSize qtyTrials]);
    fclose(file);
    
end


%total right
win=0;

noise=cell(1,nSub);
subsampleMaleStim=cell(1,nSub);
subsampleMaleAns=cell(1,nSub);
subsampleMaleRight=cell(1,nSub);
subsampleMaleWrong=cell(1,nSub);
subsampleFemaleStim=cell(1,nSub);
subsampleFemaleAns=cell(1,nSub);
subsampleFemaleRight=cell(1,nSub);
subsampleFemaleWrong=cell(1,nSub);
%subsampleConfident=cell(1,nSub);
%subsampleNonconfident=cell(1,nSub);
subsampleMaleRightConfident=cell(1,nSub);
subsampleFemaleRightConfident=cell(1,nSub);
subsampleMaleWrongConfident=cell(1,nSub);
subsampleFemaleWrongConfident=cell(1,nSub);




for sub=1:nSub
%for i=1:qtyTrials

maleStart=maleStarts(sub);
femaleStart=femaleStarts(sub);


for i=51:qtyTrials
      
   mu0=data{sub}(2,i); 
   noise{sub}(i,:)=(data{sub}(3:2+sequenceSize,i)-mu0)';
    
        %Male Stimulus
        if(data{sub}(2,i)==maleStart)
            subsampleMaleStim{sub}=[subsampleMaleStim{sub}; noise{sub}(i,:)]; 
            %Male Answer
            if(data{sub}(sequenceSize+3,i)==1)
                subsampleMaleRight{sub}=[subsampleMaleRight{sub}; noise{sub}(i,:)];
                win=win+1;
                if(data{sub}(sequenceSize+4,i)==1) %confident
                    subsampleMaleRightConfident{sub}=[subsampleMaleRightConfident{sub}; noise{sub}(i,:)];    
                end
                    
            %Female Answer
            else if(data{sub}(sequenceSize+3,i)==0)
                    subsampleMaleWrong{sub}=[subsampleMaleWrong{sub}; noise{sub}(i,:)];
                    if(data{sub}(sequenceSize+4,i)==1) %confident
                        subsampleMaleWrongConfident{sub}=[subsampleMaleWrongConfident{sub}; noise{sub}(i,:)];   
                    end
                end
            end
            
        %Female Stimulus
        else if(data{sub}(2,i)==femaleStart)
                subsampleFemaleStim{sub}=[subsampleFemaleStim{sub}; noise{sub}(i,:)];
                %Female Answer
                if(data{sub}(sequenceSize+3,i)==0)
                    subsampleFemaleRight{sub}=[subsampleFemaleRight{sub}; noise{sub}(i,:)];
                    win=win+1;
                    if(data{sub}(sequenceSize+4,i)==1) %confident
                        subsampleFemaleRightConfident{sub}=[subsampleFemaleRightConfident{sub}; noise{sub}(i,:)];    
                    end
                %end
                %Male Answer    
                else if(data{sub}(sequenceSize+3,i)==1)
                        subsampleFemaleWrong{sub}=[subsampleFemaleWrong{sub}; noise{sub}(i,:)];
                        if(data{sub}(sequenceSize+4,i)==1) %confident
                            subsampleFemaleWrongConfident{sub}=[subsampleFemaleWrongConfident{sub}; noise{sub}(i,:)];
                            
                        end
                        
                    end
                end
            end
        end
    
    if(data{sub}(sequenceSize+3,i)==1)
        subsampleMaleAns{sub}=[subsampleMaleAns{sub}; noise{sub}(i,:)]; 
    else if(data{sub}(sequenceSize+3,i)==0)
        subsampleFemaleAns{sub}=[subsampleFemaleAns{sub}; noise{sub}(i,:)];
        end
    end
        
    
%     if(data{sub}(sequenceSize+4,i)==1)
%         subsampleConfident{sub}=[subsampleConfident{sub}; noise{sub}(i,:)];
%     else subsampleNonconfident{sub}=[subsampleNonconfident{sub}; noise{sub}(i,:)];
%     end
    
    %if ((data(2,i)==maleStart && data(sequenceSize+3,i)==1) || (data(2,i)==femaleStart && data(sequenceSize+3,i)==0))
    %    win=win+1;
    %end
    success(i)=win/i;
    
    
   
end
end

frac=win/qtyTrials;

xAxis=1:sequenceSize;
megaKer=zeros(1,sequenceSize);
megaKerMaleWrong=zeros(1,sequenceSize);
megaKerMaleRight=zeros(1,sequenceSize);
megaKerFemaleWrong=zeros(1,sequenceSize);
megaKerFemaleRight=zeros(1,sequenceSize);

%init megakers fair averages
megaKerMaleWrongFair=zeros(1,sequenceSize);
amountMW=zeros(1,nSub);

megaKerMaleRightFair=zeros(1,sequenceSize);
amountMR=zeros(1,nSub);

megaKerFemaleWrongFair=zeros(1,sequenceSize);
amountFW=zeros(1,nSub);

megaKerFemaleRightFair=zeros(1,sequenceSize);
amountFR=zeros(1,nSub);

%initialize confident megaKer
megaKerMaleWrongConfident=zeros(1,sequenceSize);
megaKerMaleRightConfident=zeros(1,sequenceSize);
megaKerFemaleWrongConfident=zeros(1,sequenceSize);
megaKerFemaleRightConfident=zeros(1,sequenceSize);

%noise
megaNoise=zeros(1,sequenceSize);
megaNoiseMW=zeros(1,sequenceSize);
megaNoiseMR=zeros(1,sequenceSize);
megaNoiseFW=zeros(1,sequenceSize);
megaNoiseFR=zeros(1,sequenceSize);

%confidentMegaNoise
megaNoiseMWC=zeros(1,sequenceSize);
megaNoiseMRC=zeros(1,sequenceSize);
megaNoiseFWC=zeros(1,sequenceSize);
megaNoiseFRC=zeros(1,sequenceSize);


for sub=1:nSub
    
    % kernel total
    kernel=mean(subsampleMaleRight{sub})+mean(subsampleMaleWrong{sub})-mean(subsampleFemaleRight{sub})-mean(subsampleFemaleWrong{sub});
    megaKer=megaKer+kernel;
    
    
    % male wrong
    kernelMaleWrong=mean(subsampleMaleWrong{sub});
    megaKerMaleWrong=megaKerMaleWrong+kernelMaleWrong;
  
    %for fair, sum instead of mean
    kernelMaleWrong=sum(subsampleMaleWrong{sub})
    megaKerMaleWrongFair=megaKerMaleWrongFair+kernelMaleWrong;
    amountMW(sub)=length(subsampleMaleWrong{sub})
  
      
    % male right
    kernelMaleRight=mean(subsampleMaleRight{sub});
    megaKerMaleRight=megaKerMaleRight+kernelMaleRight;
    
    kernelMaleRight=sum(subsampleMaleRight{sub})
    megaKerMaleRightFair=megaKerMaleRightFair+kernelMaleRight;
    amountMR(sub)=length(subsampleMaleRight{sub})
  
    
    
    % female wrong
    kernelFemaleWrong=mean(subsampleFemaleWrong{sub});
    megaKerFemaleWrong=megaKerFemaleWrong+kernelFemaleWrong;
  
    kernelFemaleWrong=sum(subsampleFemaleWrong{sub})
    megaKerFemaleWrongFair=megaKerFemaleWrongFair+kernelFemaleWrong;
    amountFW(sub)=length(subsampleFemaleWrong{sub})
  
    
    % female right
    kernelFemaleRight=mean(subsampleFemaleRight{sub});
    megaKerFemaleRight=megaKerFemaleRight+kernelFemaleRight;
    
    kernelFemaleRight=sum(subsampleFemaleRight{sub})
    megaKerFemaleRightFair=megaKerFemaleRightFair+kernelFemaleRight;
    amountFR(sub)=length(subsampleFemaleRight{sub})
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%
    % Filter using confidence
    
    % male wrong confident
    kernelMaleWrongConfident=mean(subsampleMaleWrongConfident{sub});
    megaKerMaleWrongConfident=megaKerMaleWrongConfident+kernelMaleWrongConfident;
  
    % male right confident
    kernelMaleRightConfident=mean(subsampleMaleRightConfident{sub});
    megaKerMaleRightConfident=megaKerMaleRightConfident+kernelMaleRightConfident;
    
    % female wrong confident
    if(length(subsampleFemaleWrongConfident{sub})>1)
    kernelFemaleWrongConfident=mean(subsampleFemaleWrongConfident{sub});
    megaKerFemaleWrongConfident=megaKerFemaleWrongConfident+kernelFemaleWrongConfident;
    end
    
    % female right confident
    kernelFemaleRightConfident=mean(subsampleFemaleRightConfident{sub});
    megaKerFemaleRightConfident=megaKerFemaleRightConfident+kernelFemaleRightConfident;
    
     
  
  
  megaNoise=megaNoise+mean(noise{sub});
  megaNoiseMW=megaNoiseMW+mean(noise{sub}(50+randsample(qtyTrials-50,length(subsampleMaleWrong{sub})),:));
  megaNoiseMR=megaNoiseMR+mean(noise{sub}(50+randsample(qtyTrials-50,length(subsampleMaleRight{sub})),:));
  megaNoiseFW=megaNoiseFW+mean(noise{sub}(50+randsample(qtyTrials-50,length(subsampleFemaleWrong{sub})),:));
  megaNoiseFR=megaNoiseFR+mean(noise{sub}(50+randsample(qtyTrials-50,length(subsampleFemaleRight{sub})),:));
  
  %length(subsampleMaleWrong{sub})
  %noise{sub}(50+randsample(qtyTrials-50,length(subsampleMaleWrong{sub})),:)
  
  
  megaNoiseMWC=megaNoiseMWC+mean(noise{sub}(50+randsample(qtyTrials-50,length(subsampleMaleWrongConfident{sub})),:));
  megaNoiseMRC=megaNoiseMRC+mean(noise{sub}(50+randsample(qtyTrials-50,length(subsampleMaleRightConfident{sub})),:));
  if(length(subsampleFemaleWrongConfident{sub})>1)
  megaNoiseFWC=megaNoiseFWC+mean(noise{sub}(50+randsample(qtyTrials-50,length(subsampleFemaleWrongConfident{sub})),:));
  end
  megaNoiseFRC=megaNoiseFRC+mean(noise{sub}(50+randsample(qtyTrials-50,length(subsampleFemaleRightConfident{sub})),:));
  
  
end

megaKer=megaKer/nSub;

%Fair ave kernels
%amountMW
megaKerMaleWrongFair=megaKerMaleWrongFair/sum(amountMW);
megaKerMaleRightFair=megaKerMaleRightFair/sum(amountMR);
megaKerFemaleWrongFair=megaKerFemaleWrongFair/sum(amountFW);
megaKerFemaleRightFair=megaKerFemaleRightFair/sum(amountFR);



megaKerMaleWrong=megaKerMaleWrong/nSub;
megaKerMaleRight=megaKerMaleRight/nSub;
megaKerFemaleWrong=megaKerFemaleWrong/nSub;
megaKerFemaleRight=megaKerFemaleRight/nSub;

megaKerMaleWrongConfident=megaKerMaleWrongConfident/nSub;
megaKerMaleRightConfident=megaKerMaleRightConfident/nSub;
megaKerFemaleWrongConfident=megaKerFemaleWrongConfident/nSub;
megaKerFemaleRightConfident=megaKerFemaleRightConfident/nSub;

megaNoise=megaNoise/nSub;

megaNoiseMW=megaNoiseMW/nSub;
megaNoiseMR=megaNoiseMR/nSub;
megaNoiseFW=megaNoiseFW/nSub;
megaNoiseFR=megaNoiseFR/nSub;

megaNoiseMWC=megaNoiseMWC/nSub;
megaNoiseMRC=megaNoiseMRC/nSub;
megaNoiseFWC=megaNoiseFWC/nSub;
megaNoiseFRC=megaNoiseFRC/nSub;


%test plot
figure(8);
plot(xAxis,megaNoiseMW,xAxis,megaKerMaleWrongFair);

%Some output..

%subsampleMaleWrongConfident;
%subsampleFemaleWrongConfident;

%subsampleMaleWrongConfident{1}

%size(subsampleMaleRight)
%size(subsampleMaleWrong)
%size(subsampleFemaleRight)
%size(subsampleFemaleWrong)


%plot(xAxis,megaNoise,xAxis,megaKer);

%%%%%% Kernels based on answer %%%%%%
figure(7)
axisVector = [0 20 -1.5 1.5];
subplot(2,2,1);
plot(xAxis,megaNoiseMW,xAxis,megaKerMaleWrongFair);
title('Male Wrong');
xlabel('Nro. de secuencia'); ylabel('�ndice de femeneidad (normalizado)');
legend('noise','kernel');
axis(axisVector);

subplot(2,2,2);
plot(xAxis,megaNoiseMR,xAxis,megaKerMaleRightFair);
title('Male Right');
xlabel('Nro. de secuencia'); ylabel('�ndice de femeneidad (normalizado)');
legend('noise','kernel');
axis(axisVector);

subplot(2,2,3);
plot(xAxis,megaNoiseFW,xAxis,megaKerFemaleWrongFair);
title('Female Wrong');
xlabel('Nro. de secuencia'); ylabel('�ndice de femeneidad (normalizado)');
legend('noise','kernel');
axis(axisVector);

subplot(2,2,4);
plot(xAxis,megaNoiseFR,xAxis,megaKerFemaleRightFair);
title('Female Right');
xlabel('Nro. de secuencia'); ylabel('�ndice de femeneidad (normalizado)');
legend('noise','kernel');
axis(axisVector);
%%%%%% end Kernels based on answer %%%%%%

%%% Confidence Plots %%%
axisVector = [0 20 -1.5 1.5];
figure(3)
subplot(2,2,1);
plot(xAxis,megaNoiseMWC,xAxis,megaKerMaleWrongConfident);
title('Male Wrong Confident');
xlabel('Nro. de secuencia'); ylabel('�ndice de femeneidad (normalizado)');
legend('noise','kernel');
axis(axisVector);

subplot(2,2,2);
plot(xAxis,megaNoiseMRC,xAxis,megaKerMaleRightConfident);
title('Male Right Confident');
xlabel('Nro. de secuencia'); ylabel('�ndice de femeneidad (normalizado)');
legend('noise','kernel');
axis(axisVector);

subplot(2,2,3);
plot(xAxis,megaNoiseFWC,xAxis,megaKerFemaleWrongConfident);
title('Female Wrong Confident');
xlabel('Nro. de secuencia'); ylabel('�ndice de femeneidad (normalizado)');
legend('noise','kernel');
axis(axisVector);

subplot(2,2,4);
plot(xAxis,megaNoiseFRC,xAxis,megaKerFemaleRightConfident);
title('Female Right Confident');
xlabel('Nro. de secuencia'); ylabel('�ndice de femeneidad (normalizado)');
legend('noise','kernel');
axis(axisVector);
%%% end Confidence plots %%%


%%%% Randomized sequences %%%%
figure(2)
axisVector = [0 20 -4 4];
subplot(2,2,1);
%plot(xAxis,megaKerMaleWrong,xAxis,randsample(megaKerMaleWrong, length(megaKerMaleWrong)));
plot(xAxis,megaNoiseMW,xAxis,randsample(megaKerMaleWrong, length(megaKerMaleWrong)));
title('Male Wrong');
xlabel('Nro. de secuencia'); ylabel('�ndice de femeneidad (normalizado)');
legend('kernel','random kernel');
axis(axisVector);

subplot(2,2,2);
plot(xAxis,megaKerMaleRight,xAxis,randsample(megaKerMaleRight, length(megaKerMaleRight)));
title('Male Right');
xlabel('Nro. de secuencia'); ylabel('�ndice de femeneidad (normalizado)');
legend('kernel','random kernel');
axis(axisVector);

subplot(2,2,3);
%plot(xAxis,megaKerFemaleWrong,xAxis,randsample(megaKerFemaleWrong, length(megaKerFemaleWrong)));
plot(xAxis,megaNoiseFW,xAxis,randsample(megaKerFemaleWrong, length(megaKerFemaleWrong)));
title('Female Wrong');
xlabel('Nro. de secuencia'); ylabel('�ndice de femeneidad (normalizado)');
legend('kernel','random kernel');
axis(axisVector);

subplot(2,2,4);
plot(xAxis,megaKerFemaleRight,xAxis,randsample(megaKerFemaleRight, length(megaKerFemaleRight)));
title('Female Right'); 
xlabel('Nro. de secuencia'); ylabel('�ndice de femeneidad (normalizado)');
legend('kernel','random kernel');
axis(axisVector);
%%%% End Randomized Sequences %%%%


%%%%%%%%%%%%%%%%%%
%FIGURES TO SHOW
%
figure(4)
axisVector = [0 760 -1.4 1.4];
%subplot(2,1,1);
tAxis=(xAxis-1)*40;
plot(tAxis,megaNoiseMW,tAxis,megaKerMaleWrong);
title('Estímulo HOMBRE, Respuesta MUJER');
xlabel('Tiempo (ms)'); ylabel('Virilidad Negativa Relativa');
legend('ruido','kernel');
axis(axisVector);

figure(5)
%subplot(2,1,2);
plot(tAxis,megaNoiseFW,tAxis,megaKerFemaleWrong);
title('Estímulo MUJER, Respuesta HOMBRE');
xlabel('Tiempo (ms)'); ylabel('Virilidad Negativa Relativa');
legend('ruido','kernel');
axis(axisVector);


%%%% Other plots %%%%
%plot(xAxis,mean(subsampleMaleStim),xAxis,mean(subsampleMaleRight)-mean(subsampleFemaleWrong));
%plot(xAxis,mean(subsampleMaleStim),xAxis,mean(subsampleMaleRight)-mean(subsampleFemaleWrong),xAxis,mean(subsampleMaleRight)-mean(subsampleFemaleWrong)-mean(subsampleMaleStim));
%plot(xAxis,mean(noise),xAxis,mean(subsampleConfident)-mean(subsampleNonconfident));

%size(subsampleConfident)
%mean(subsampleConfident(:))
%size(subsampleNonconfident)
%mean(subsampleNonconfident(:))

%mean(subsampleMaleRight(:))
%mean(subsampleMaleWrong(:))
%mean(subsampleFemaleWrong(:)) 
%mean(subsampleFemaleRight(:))

%trials=1:qtyTrials;
%plot(trials,success);
%calibIndices=mean(data(3:2+sequenceSize,:))
%calibAnswers=data(sequenceSize+3,:);
   
%hist(mean(noise').*data(sequenceSize+3,:));
 
%plot(calibIndices,calibAnswers,'x');
% 
% qtyWindows=20;
% step=1;
% for winIndex=1:qtyWindows
%     
%     trialsInWin(winIndex)=0;
%     malesInWin(winIndex)=0;
%     window=15+(winIndex)*step;
%     windows(winIndex)=window;
%     
%     
%     for trial=1:qtyTrials
%         if(calibIndices(trial)>= window && calibIndices(trial) < window+step)
%             trialsInWin(winIndex)=trialsInWin(winIndex)+1;
%                 if(calibAnswers(trial)==1)
%                     malesInWin(winIndex)=malesInWin(winIndex)+1;
%                 end
%         end
%     end
%     
%     fracInWin(winIndex)=malesInWin(winIndex)/trialsInWin(winIndex);
%     
% end
% 
% trialsInWin
% malesInWin
%plot(windows,fracInWin,'x');




end

